﻿
package extremefx.modules {
	import extremefx.events.EventHandler;	
	
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;	

	/**
	 * @author Marcelo Volmaro
	 */
	internal class BasePlayer {
		protected var _soundTransform:SoundTransform;
		protected var _soundChannel:SoundChannel;
		protected var _volume:Number;
		protected var _isPlaying:Boolean;
		protected var _position:Number;
		protected var _firstPlay:Boolean;
		protected var _url:String;
		protected var _loop:Boolean;
		protected var _metadata:Object;
		
		protected var _bufferLength:Number; 
		
		public function BasePlayer() {
			_soundChannel = new SoundChannel();
			_volume = 100;
			_isPlaying = _loop = false;
			_position = 0;
			_firstPlay = true;
			
			_soundTransform = new SoundTransform();
		}
		 
		public function get bufferLength():Number { 
			return _bufferLength; 
		} 
		
		public function get loop():Boolean{
			return _loop;
		}
		
		public function set loop(pLoop:Boolean):void{
			_loop = pLoop;
		}
		
		public function get volume():Number{
			return _volume;
		}
		
		protected var _eOnOpen:EventHandler;
		/**
		 * Fires when the connection if first open
		 * eventArgs[0] contains the actual error (IOErrorEvent)
		 */
		public function get onOpen():EventHandler{
			if (_eOnOpen == null) _eOnOpen = new EventHandler(this);
			return _eOnOpen;
		}
		
		protected var _eOnMetaData:EventHandler;
		/**
		 * Fires if the sound stream contains metadata and after the player has loaded it.
		 * eventArgs[0] contains the metadata
		 */
		public function get onMetaData():EventHandler{
			if (_eOnMetaData == null) _eOnMetaData = new EventHandler(this);
			return _eOnMetaData;
		}
		
		protected var _eOnProgress:EventHandler;
		/**
		 * Fires while the stream is loading
		 * eventArgs[0] contains the actual progress (0 - 1)
		 */
		public function get onProgress():EventHandler{
			if (_eOnProgress == null) _eOnProgress = new EventHandler(this);
			return _eOnProgress;
		}
		
		protected var _eOnComplete:EventHandler;
		/**
		 * Fires when the stream has been completelly loaded
		 */
		public function get onComplete():EventHandler{
			if (_eOnComplete == null) _eOnComplete = new EventHandler(this);
			return _eOnComplete;
		}
		
		protected var _eOnPlayComplete:EventHandler;
		/**
		 * Fires when the playhead reached the end of the stream
		 */
		public function get onPlayComplete():EventHandler{
			if (_eOnPlayComplete == null) _eOnPlayComplete = new EventHandler(this);
			return _eOnPlayComplete;
		}
		
		protected var _eOnError:EventHandler;
		/**
		 * Fires if errors are detected while loading the stream
		 */
		public function get onError():EventHandler{
			if (_eOnError == null) _eOnError = new EventHandler(this);
			return _eOnError;
		}
	}
}
